package david.java.tools;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 校验两个表的字段是不是一致
 * @Author: ZhaoDawei
 * @Date: Create in 3:34 下午 2023/6/30
 */
public class VerifyTableDataSql extends BaseUtil{
    private final static String SOURCE_PATH = "src/tmp/src_col_verify";
    private final static String TARGET_PATH = "target-tmp/tar_col_verify";


    public static void main(String[] args) throws IOException {
        List<List<String>> col = extractCol();
        assert col.get(0).size() == col.get(1).size();


        String verifySql = createVerifySql(col);
        System.out.println(verifySql);

        mkdirIfNotExists(TARGET_PATH);
        writeToPath(TARGET_PATH, verifySql);


    }


    private static List<List<String>> extractCol() throws IOException {
        List<List<String>> list = new ArrayList<>();    // 0 为第一列, 1 为第二列
        List<String> cols = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(SOURCE_PATH));
        String line;
        boolean isBlank = false;

        while ((line = br.readLine()) != null) {
            if (StringUtils.isBlank(line)) {   // 遇到了空行
                if (!isBlank) {
                    list.add(cols);
                    cols = new ArrayList<>();
                    isBlank = true;
                }
            } else {
                String lineTrimed = line.trim();
                if (lineTrimed.startsWith("--")) {
                    continue;
                }
                // String  word = getAliasOrCol(lineTrimed);
                String word = getColFromDDl(lineTrimed);

                char[] chars = word.toCharArray();
                StringBuilder sb = new StringBuilder();
                for (char c : chars) {
                    if (c != '`' && c != ',') {
                        sb.append(c);
                    }
                }
                cols.add(sb.toString());
                isBlank = false;
            }
        }
        list.add(cols);
        return list;
    }

    private static String createVerifySql(List<List<String>> lists) {
        List<String> l1 = lists.get(0);
        List<String> l2 = lists.get(1);
        String blankLeft = leftPad(8)  ;
        String blankBeforeAs = "\t\t\t";
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT   count(*)").append(blankBeforeAs).append("AS totol_cnt").append("\n");

        for (int i = 0; i < l1.size(); i++) {
            sb.append(blankLeft).append(",").append("count(*) - count(if(coalesce(t1.").append(l1.get(i)).append(", '-999999') = coalesce(t2.").append(l2.get(i)).append(", '-999999'),1,null))").append(blankBeforeAs).append("AS ").append(l1.get(i)).append("_diff").append("\n");
        }
        sb.append("\n");
        sb.append("on  coalesce(t1. , '-999999') = coalesce(t2. , '-999999')");
        sb.append("\n");
        sb.append("and coalesce(t1. , '-999999') = coalesce(t2. , '-999999')");
        return sb.toString();
    }
}

